<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var miceAndCheese = function (reward1, reward2, k) {
      let ans = 0;
      let n = reward1.length;
      let diffs = new Array(n);
      for (let i = 0; i < n; i++) {
        ans += reward2[i];
        diffs[i] = reward1[i] - reward2[i];
      }
      diffs.sort((a, b) => a - b);
      for (let i = 1; i <= k; i++) {
        ans += diffs[n - i];
      }
      return ans;
    }
    console.log(miceAndCheese(reward1 = [1, 1, 3, 4], reward2 = [4, 4, 1, 1], k = 2))
  </script>
</body>

</html>